package com.boarsoft.boar.lib.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.boar.entity.AppLib;
import com.boarsoft.boar.lib.LibAppBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

/**
 * 根据libId查AppInfo
 * 
 * @author Mac_J
 *
 */
@Component("libAppBiz")
public class LibAppBizImpl extends SimpleBizImpl implements LibAppBiz {
	/**
	 * 根据库包Id查询其关联的所有应用
	 */
	@Override
	@Transactional(readOnly = true)
	public PagedResult<AppInfo> list(String libId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(AppLib.class.getName());
		sb.append(" o join o.app a where o.libId='").append(libId).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (a.code like '%").append(key);
			sb.append("%' or a.name like '%").append(key);
			sb.append("%' or a.memo like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal("select count(a.id)".concat(sb.toString()));
		if (Util.strIsNotEmpty(orderBy)) {
			String ob = Util.array2str(("l.".concat(orderBy))//
					.replaceAll(",", ",").split(","), ",");
			sb.append(" order by ").append(ob);
		} else {
			sb.append(" order by l.name");
		}
		List<AppInfo> lt = dao.list("select a".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<AppInfo>(total, lt, pageNo, pageSize);
	}

	/**
	 * 根据库包Id查询其关联的所有应用
	 */
	@Override
	@Transactional(readOnly = true)
	public List<AppInfo> list(String libId) {
		StringBuilder sb = new StringBuilder();
		sb.append("and l.id='").append(libId).append("'");
		String ql = new StringBuilder().append("select distinct a from ")//
				.append(AppInfo.class.getName()).append(" a join a.libs l")//
				.append(" where 1=1 ").append(sb).toString();
		return dao.list(ql);
	}
}